package cn.swing.ushare.user.webapi.controller;

import cn.swing.ushare.common.web.JsonResult;
import cn.swing.ushare.pojo.dto.UserLoginDTO;
import cn.swing.ushare.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping(value = "/admins",produces = "application/json;charset=utf-8")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/login")
//    @PreAuthorize("hasAnyAuthority('登录后台')")
    public JsonResult<String> login(String password,String username){
        UserLoginDTO userLoginDTO = new UserLoginDTO();
        userLoginDTO.setPassword(password);
        userLoginDTO.setUsername(username);
        System.out.println("LoginController.login()");
        System.out.println("userLoginDTO = " + userLoginDTO);
        String jwt = loginService.login(userLoginDTO);
        System.out.println(jwt);
        return JsonResult.ok(jwt);
    }

    @GetMapping("/loginBefore")
    public JsonResult<String> loginBefore(String username){
        return JsonResult.ok(loginService.loginBefore(username));
    }
}
